unit uAppLog;

interface

uses
  SysUtils, Windows, uDZSysUtils, uDZThread, uDZTrace,
  uDZLogWritter, uDZProcess, uAppCmdLineParams;

procedure WriteLog(sev: TDzTraceSeverity; const s: string);
procedure FmtWriteLog(sev: TDzTraceSeverity; const fmt: string;
  const args: array of const);
procedure LogException(const func: string; E: Exception);

implementation

var
  CriticalSection: TDzCriticalSection;
  LogWriter: IDzTrace;

procedure LogException(const func: string; E: Exception);
begin
  WriteLog(tsError, Format('%s(%s): %s'#10, [func, E.ClassName, E.Message]));
end;

procedure WriteLog(sev: TDzTraceSeverity; const s: string);
begin
  CriticalSection.Acquire;
  try
    LogWriter.Write(sev, s);
  finally
    CriticalSection.Release;
  end;
end;

procedure FmtWriteLog(sev: TDzTraceSeverity; const fmt: string;
  const args: array of const);
begin
  CriticalSection.Acquire;
  try
    LogWriter.FormatWrite(sev, fmt, args);
  finally
    CriticalSection.Release;
  end;
end;

initialization
  CriticalSection := TDzCriticalSection.Create;
  if cmdLineParams.LogType = altConsole then
  begin
    Windows.AllocConsole;
    LogWriter := TConsoleTrace.Create;
  end
  else
    LogWriter := TDzMultiFileLogWritter.Create(ExtractFilePath(ParamStr(0)) + 'logs');
  LogWriter.Attributes['Encoding'] := teAnsi;
  LogWriter.Attributes['LogSeparate'] := dtpHour;
  LogWriter.DateTimeFormat := 'hh:nn:ss';
  LogWriter.Options := [toTimeTick];
  if CmdLineParams.DebugMode then
    LogWriter.Severity := TRACE_SEVERITIES_ALL
  else LogWriter.Severity := TRACE_SEVERITIES_ALL - [tsDebug];

finalization
  CriticalSection.Free;

end.
